import React from 'react';
import CommonList from '../../../components/CommonLists/list';
import { Button, message } from 'antd';
import Modal from '../../../components/Modal/index';
import services from '../../../services/api';
import EditIndex from './Edit/index';

class NameList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'nameList';
    this.editButton = (record) => {
      return (
        <Button
          type='link'
          onClick={() => props.history.push('/variable/nameList/' + record.name_list_id + '/edit')}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          编辑
        </Button>
      );
    };
    this.delButton = (record) => {
      return (
        <Button
          type='link'
          onClick={this.delete.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          删除
        </Button>
      );
    };

    this.downloadButton = (record) => {
      return (
        <Button
          type='link'
          onClick={this.download.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          下载
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.delButton, this.downloadButton];
    this.state = {
      namelist_type: {},
      namelist_data_type: {},
    };
  }

  // 删除时的参数
  getDeleteParam = (record) => {
    return { name_list_id: record.name_list_id };
  };

  download = (record) => {
    Modal.open({
      title: '导出数据',
      width: '800px',
      content: <EditIndex {...this.props} id={record.name_list_id} onlyExport={true} />,
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        Modal.close();
      },
    });
  };

  // componentWillMount() {
  //   const namelist_type = {};
  //   this.props.namelist_type.forEach(item => {
  //     namelist_type[item.id] = item.value;
  //   });
  //   const namelist_data_type = {};
  //   this.props.namelist_data_type.forEach(item => {
  //     namelist_data_type[item.id] = item.value;
  //   });
  //   this.setState({
  //     namelist_type,
  //     namelist_data_type
  //   });
  // }

  refreshTableFilter = (params) => {
    this.props.refreshTableFilter && this.props.refreshTableFilter(params);
  };

  delete(record) {
    if (record.strategy_count > 0) {
      message.error(`有${record.strategy_count}条策略使用了此名单，请先下线关联策略再删除此名单！`);
    } else {
      Modal.open({
        title: '',
        isConfirm: true,
        width: '300px',
        content: '确定要删除?',
        isOkBtn: true,
        isCancelBtn: true,
        ok: () => {
          services[this.api].delete(this.getDeleteParam(record)).then((data) => {
            if (data.code === 0) {
              message.success(data.msg);
              this.props.queryList();
              Modal.close();
            } else {
              message.error(data.msg);
            }
          });
        },
        cancel: () => {
          Modal.close();
        },
      });
    }
  }
}

export default NameList;
